package com.lagou.esweb.runner;

import com.lagou.esweb.mvc.doc.PositionDoc;
import com.lagou.esweb.mvc.entity.PositionDo;
import com.lagou.esweb.mvc.mapper.PositionMapper;
import com.lagou.esweb.mvc.repository.PositionRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Component
@RequiredArgsConstructor
public final class DataInitRunner implements ApplicationRunner {

    final PositionMapper positionMapper;

    final PositionRepository positionRepository;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("开始初始化数据...");
        List<PositionDo> positionDoList = positionMapper.selectList(null);
        positionRepository.saveAll(positionDoList.stream().map(this::convert).collect(Collectors.toList()));
        log.info("初始化数据完成...");
    }

    private PositionDoc convert(PositionDo positionDo) {
        PositionDoc doc = new PositionDoc();
        BeanUtils.copyProperties(positionDo, doc);
        return doc;
    }
}
